from PySide6.QtCore import Qt, Signal
from qfluentwidgets import SettingCard, FluentIconBase, DoubleSpinBox
from qfluentwidgets import qconfig

class DoubleSpinBoxSettingCard(SettingCard):
    """ 浮点数值输入设置卡片 """
    
    valueChanged = Signal(float)

    def __init__(self, icon: FluentIconBase, title: str, content: str, 
                 configItem, parent=None):
        super().__init__(icon, title, content, parent)
        self.configItem = configItem
        
        # 创建浮点数值输入框
        self.spinBox = DoubleSpinBox(self)
        self.spinBox.setValue(qconfig.get(self.configItem))
        self.spinBox.setMinimumWidth(160)
        self.spinBox.setSingleStep(0.1)
        # 将数值输入框添加到右侧
        self.hBoxLayout.addWidget(self.spinBox, 0, Qt.AlignRight)
        self.spinBox.valueChanged.connect(self._onValueChanged)

    def _onValueChanged(self, value: float):
        """ 值改变时的处理 """
        qconfig.set(self.configItem, value)
        self.valueChanged.emit(value)

    def getValue(self):
        """ 获取当前值 """
        return self.spinBox.value()

    def setValue(self, value: float):
        """ 设置值 """
        self.spinBox.setValue(value)
        qconfig.set(self.configItem, value) 